<%@ page import="vo.Teacher" %>
<%@ page import="vo.Attendance" %>
<%@ page import="dao.AttendanceDao" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dao.StudentDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="../resources/css/jquery-ui-1.10.4.custom.min.css">
    <script src="../resources/js/jquery-1.10.2.js"></script>
    <script src="../resources/js/jquery-ui-1.10.4.custom.min.js"></script>
    <title>main</title>
    <link href="../resources/css/default.css" rel="stylesheet"/>
</head>
<body>
<%
    Teacher teacher = (Teacher) session.getAttribute("info");
    int sumIndex = (int) session.getAttribute("sumIndex");
    String time;
    if(request.getParameter("attendance_time") != null){
        time = request.getParameter("attendance_time");
    }else{
        time = session.getAttribute("attendance_time").toString();
    }
%>
<div id="page" class="container">
    <div id="header">
        <div id="logo">
            <img src="../userImg/<%=teacher.getId()%>.jpeg"/>
            <h1><%=teacher.getId()%></h1>
        </div>
        <div id="menu">
            <ul>
                <li><a href="personal.jsp">个人信息</a></li>
                <li><a href="../one_page_student">学生管理</a></li>
                <li><a href="../one_page_course">课程管理</a></li>
                <li class="current_page_item"><a href="../one_page_attendance?teacher_id=<%=teacher.getId()%>">出勤管理</a></li>
                <li><a href="../one_page_score">成绩管理</a></li>
                <li><a href="../one_page_tuition_records">学费管理</a></li>
                <li><a onclick="return confirm('确认退出?');" href="../exit">退出登录</a></li>
            </ul>
        </div>
    </div>
    <div id="main">
        <div class="top">
            <h2>学生出勤管理</h2>
            <hr/>
            <button class="ui-icon-arrowreturn-1-e" onclick="location.href='../one_page_attendance?teacher_id=<%=teacher.getId()%>'" style="margin-right: 750px; margin-top: auto">返回</button>
            <form action="attendance_detail_excel.jsp" method="post">
                <input type="submit" value="导出excel" style="margin-left: 750px;margin-top: auto">
                <input type="hidden" name="attendance_time" value="<%=time%>">
                <input type="hidden" name="teacher_id" value="<%=teacher.getId()%>">
            </form>
        </div>
        <div class="table" style="margin-top:20px;height: 525px">
            <table id=table width="800" frame="box" align="center">
                <tr>
                    <th height="35">姓名</th>
                    <th>学号</th>
                    <th>出勤情况</th>
                </tr>
                <%
                    try {

                        AttendanceDao attendanceDao = new AttendanceDao();
                        List<Attendance> attendances = new ArrayList<>();
                        StudentDao studentDao = new StudentDao();
                        String attendanceTime;
                        String teacherId;
                        if(session.getAttribute("attendances") != null && request.getParameter("attendance_time") == null) {
                            attendances = (List<Attendance>) session.getAttribute("attendances");
                            attendanceTime = session.getAttribute("attendance_time").toString();
                            teacherId = session.getAttribute("teacherId").toString();
                        } else {
                            attendanceTime = request.getParameter("attendance_time");
                            teacherId = request.getParameter("teacher_id");
                            attendances = attendanceDao.getAttendanceByTimeAndTeacherId(attendanceTime, teacherId);
                        }

                        for (Attendance attendance : attendances) {
                            String name = studentDao.findWithId(attendance.getStu_id()).getName();
                            String id = attendance.getStu_id();
                            String status = String.valueOf(attendance.getStatus());
                %>
                <tr>
                    <td height="35"><%=name%></td>
                    <td><%=id%></td>
                    <td>
                        <form id="attendanceForm<%= id %>" action="../updateAttendance" method="post">
                            <select name="attendanceStatus">
                                <option value="到课" <% if(status.equals("到课")) { %>selected<% } %>>到课</option>
                                <option value="缺勤" <% if(status.equals("缺勤")) { %>selected<% } %>>缺勤</option>
                                <option value="请假" <% if(status.equals("请假")) { %>selected<% } %>>请假</option>
                            </select>
                            <input type="hidden" name="stu_id" value="<%=attendance.getStu_id()%>">
                            <input type="hidden" name="attendance_Time" value="<%=attendanceTime%>">
                            <input type="hidden" name="teacherId" value="<%=teacherId%>">
                            <input type="submit" value="更新">
                        </form>
                    </td>
                </tr>
                <%
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                %>
            </table>
        </div>

        <%
            if (sumIndex > 1){
        %>
        <div id="index">
            <a href="../one_page_attendance_detail?index=1&teacherId=<%=teacher.getId()%>&attendance_time=<%=time%>">首页</a>
            <%
                for (int i = 1; i <= sumIndex; i++) {
            %>
            <a href="../one_page_attendance_detail?index=<%=i%>&teacherId=<%=teacher.getId()%>&attendance_time=<%=time%>">第<%=i%>页</a>
            <%
                }
            %>
            <a href="../one_page_attendance_detail?index=<%=sumIndex%>&teacherId=<%=teacher.getId()%>&attendance_time=<%=time%>">尾页</a>
        </div>
        <%
            }
        %>

    </div>
</div>

<style>
    .ui-dialog-titlebar-close {
        display: none
    }
    .top button {
        margin-right: 10px;
    }
</style>

<script>

</script>
</body>
</html>
